<?php
include ("global.php");

if($configarray[40]){
	$links=" > User controls";
	$pagetitle=" - User controls";
	include("header.php");
	tableheader1();
	print "<tr><td class=\"tablecell1\"><span class=\"textlarge\">";
	print "<b>Board closed:</b><br>$configarray[40]";
	print "</span></td></tr></table>";
}else{
	
	if(!$user){$user=$useridarray[$navboardlogin];}
	
	$user=(float) $user;
	$useridarray[$navboardlogin]=(float) $useridarray[$navboardlogin];
	
	//not logged in || not own profile and not admin || not own profile
	if(($user!==$useridarray[$navboardlogin])&&($userloggedinarray[15]!=="administrator")||$login!==1){
		$links=" > User controls";
		$pagetitle=" - User controls";
		
		include("header.php");
		include("user_header.php");
		
		echo template('notice',array('notice'=>'You do not have access to this users controls!'));
		
	}else{
		
		createdir("$configarray[1]/$user/pms");
		$userarray=getdata("$configarray[1]/$user/main.php");
		
		$links=" > User controls > $userarray[0]";
		$pagetitle=" - User controls - $userarray[0]";
		
		include("header.php");
		include("user_header.php");
		
		$tplcontent['inbox'].="user_pm.php?mode=inbox&user=$user";

		$tplcontent['send'].="user_pm.php?mode=send&user=$user";

		$total=checkpmspace($user);
		
		$tplcontent['bytespercentbar'].=percentbar($total['percentsize'],'100%');
		$tplcontent['bytes'].=$total['size'];
		$tplcontent['bytesmax'].=$configarray[29];
		$tplcontent['bytespercent'].=$total['percentsize']."%";

		$tplcontent['amountpercentbar'].=percentbar($total['percentnumber'],'100%');
		$tplcontent['amount'].=$total['number'];
		$tplcontent['amountmax'].=$configarray[30];
		$tplcontent['amountpercent'].=$total['percentnumber']."%";
		
		echo template('userpmheader',$tplcontent);
		
		if($mode==""||$mode=="inbox"){
			
			$pmlistarray=listfiles("$configarray[1]/$user/pms");
			@rsort($pmlistarray,SORT_NUMERIC);
			
			for($n=0;$n<count($pmlistarray);$n++){
				unset($tplcontent2);
				
				$pmarray=getdata("$configarray[1]/$user/pms/$pmlistarray[$n].php");
				
				$tplcontent2['subject'].="<a href=\"user_pm.php?mode=view&message=$pmlistarray[$n]&user=$user\">$pmarray[2]</a>";
				
				$userarray=getdata("$configarray[1]/$pmarray[0]/main.php");
				if(count($userarray)>0){
					$tplcontent2['user'].="<a href=\"profile.php?user=$pmarray[0]\">$userarray[0]</a>";
				}else{
					$tplcontent2['user'].="Guest";
				}

				$date=date($dateformat,$pmarray[1]+$timeoffsetseconds);
				$tplcontent2['date'].=$date;
				
				$tplcontent2['delete'].="user_pm.php?mode=delete&message=$pmlistarray[$n]&user=$user";
				
				if($pmarray2[8]!=="unread"){
					$tplcontent2['read'].="X";
				}else{
					$tplcontent2['read'].="&nbsp";
				}
				
				$tplcontent['pmrows'].=template('userpmlistrow',$tplcontent2);
			}
			
			echo template('userpmlist',$tplcontent);
			
		}elseif($mode=="send"){
			
			if($step==""){
				unset($tplcontent);
				$pmarray=getdata("$configarray[1]/$user/pms/$message.php");
				
				$tplcontent['formheader'].="<form action=\"user_pm.php\" method=post name=\"post\">";
				$tplcontent['formheader'].="<input type=hidden name=\"step\" value=\"2\">";
				$tplcontent['formheader'].="<input type=hidden name=\"mode\" value=\"$mode\">";
				$tplcontent['formheader'].="<input type=hidden name=\"user\" value=\"$user\">";
				
				$tplcontent['sendto'].="<tr>";
				$tplcontent['sendto'].="<td class=\"tablecell1\" colspan=\"2\"><span class=\"textlarge\">Send to</span><br>";
				$tplcontent['sendto'].="<select name=\"sendto\" class=\"forminput\">";
				if($pmarray[0]){$selected=$pmarray[0];}
				$tplcontent['sendto'].=usersmenu("$selected");
				$tplcontent['sendto'].="</select>";
				$tplcontent['sendto'].="</td>";
				$tplcontent['sendto'].="</tr>";
				
				$tplcontent['subject'].="<tr>";
				$tplcontent['subject'].="<td class=\"tablecell1\" colspan=\"2\">";
				if($pmarray[2]){
					$tplcontent['subject'].="<input type=text name=\"subject\" value=\"Re: $pmarray[2]\" class=\"forminput\" size=40>";
				}else{
					$tplcontent['subject'].="<input type=text name=\"subject\" class=\"forminput\" size=40>";
				}
				$tplcontent['subject'].="</td>";
				$tplcontent['subject'].="</tr>";
				
				$tplcontent['body'].="<tr>";
				$tplcontent['body'].="<td width=\"30%\" class=\"tablecell1\" valign=\"top\"><span class=\"textlarge\">";
				$tplcontent['body'].=displaysmilies();
				$tplcontent['body'].="</span>";
				$tplcontent['body'].="</td>";
				$tplcontent['body'].="<td width=\"70%\" class=\"tablecell1\">";
				$tplcontent['body'].="<textarea class=\"forminput\" rows=12 cols=80 name=\"body\" class=\"forminput\">$value</textarea><br>";
				$tplcontent['body'].="</td>";
				$tplcontent['body'].="</tr>";
				
				$tplcontent['submitbutton'].="<input type=submit name=\"submit\" value=\"Send\" class=\"formbutton\">";
				$tplcontent['formfooter'].="</form>";
				
				echo template('userpmsend',$tplcontent);
			}elseif($step=="2"){
				
				$tplcontent['notice']=sendpm($sendto,$user,$subject,$body);
				echo template('notice',$tplcontent);
				
				echo "<br>";
				
			}//send step check
			
		}elseif($mode=="view"&&isset($message)){
			unset($tplcontent);
			
			$pmarray=getdata("$configarray[1]/$user/pms/$message.php");
			writedata("$configarray[1]/$user/pms/$message.php","read",8);
			
			$tplcontent['title'].="Message #$message";
			
			$userarray=getdata("$configarray[1]/$pmarray[0]/main.php");
			if(count($userarray[0])>0){
				$tplcontent['from'].="<a href=\"profile.php?user=$pmarray[0]\">$userarray[0]</a>";
			}else{
				$tplcontent['from'].="Guest";
			}
			
			$date=date($dateformat,$pmarray[1]+$timeoffsetseconds);
			$tplcontent['date'].="$date";
			
			$pmarray[2]=stripslashes($pmarray[2]);
			$pmarray[2]=htmlentities($pmarray[2]);
			$tplcontent['subject'].="$pmarray[2]";
			
			$body=bodyparse($pmarray[3]);
			$tplcontent['body'].="$body";
			
			if(count($userarray[0])>0){
				$tplcontent['reply'].="<a href=\"user_pm.php?mode=send&message=$message\"><img src=\"$theme_imagespath/reply.gif\" border=0></a>";
			}else{
				$tplcontent['reply'].="Cannot reply";
			}
			
			echo template('userpmview',$tplcontent);
			
		}elseif($mode=="delete"||isset($message)){
			@unlink("$configarray[1]/$user/pms/$message.php");
			
			$tplcontent['notice']="PM '$message' deleted";
			
			echo template('notice',$tplcontent);
			echo "<br>";
		}
		
	}//access check
	
}

require ("footer.php");
?>
